﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Vehicle_Maintenance
{
    public partial class Gas_Log : Form
    {
        private bool dPoint = false;
        private bool COGDPoint = false;
        private bool galDPoint = false;

        public Gas_Log(bool update = false, string vin = "", string date = "", string odom = "")
        {
            InitializeComponent();
            if (update)
            {
                DataTable data = this.gasFillUpsTableAdapter.GetDataByGasInfo(vin, Convert.ToDateTime(date), Convert.ToDouble(odom));

                if (data.Rows.Count == 1)
                {
                    DataRow dRow = data.Rows[0];
                    tb_Odom.Text = dRow["Odom"].ToString();
                    dt_GasDate.Value = Convert.ToDateTime(dRow["DateTime"].ToString());
                    double COG = Convert.ToDouble(dRow["Cost_Gal"].ToString());
                    double gal = Convert.ToDouble(dRow["Gal"].ToString());
                    double total = COG * gal;
                    tb_COG.Text = COG.ToString();
                    tb_Gal.Text = gal.ToString();
                    l_Total.Text = total.ToString();
                    int octane = Convert.ToInt32(dRow["Octane"].ToString());
                    switch (octane)
                    {
                        case 1:
                            rb_Reg.Checked = true;
                            break;
                        case 2:
                            rb_MG.Checked = true;
                            break;
                        case 3:
                            rb_P.Checked = true;
                            break;
                        case 4:
                            rb_D.Checked = true;
                            break;
                    }
                    if (dRow["Loc"].ToString() != "")
                    {
                        tb_Location.Text = dRow["Loc"].ToString();
                    }
                    if (dRow["Gas_Brand"].ToString() != "")
                    {
                        tb_GasBrand.Text = dRow["Gas_Brand"].ToString();
                    }
                    if (dRow["Pay_type"].ToString() != "")
                    {
                        for (int i = 0; i < cb_PayType.Items.Count; i++)
                        {
                            if (cb_PayType.Items[i].Equals(dRow["Pay_type"].ToString()))
                            {
                                cb_PayType.SelectedIndex = i;
                                break;
                            }
                            if (i == cb_PayType.Items.Count - 1)
                            {
                                cb_PayType.SelectedIndex = i;
                                tb_Other.Text = dRow["Pay_type"].ToString();
                            }
                        }
                    }
                }
            }
        }

        private void b_OK_Click(object sender, EventArgs e)
        {
            if (tb_Odom.Text == "")
            {
                MessageBox.Show("Odometer is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (tb_COG.Text == "")
            {
                MessageBox.Show("Cost per gallon is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (tb_Gal.Text == "")
            {
                MessageBox.Show("Gallons", "Required Field", MessageBoxButtons.OK);
            }
            else if (rb_Reg.Checked == false && rb_MG.Checked == false && rb_P.Checked == false && rb_D.Checked == false)
            {
                MessageBox.Show("Must select one of the Octanes", "Required Field", MessageBoxButtons.OK);
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void b_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void cb_PayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_PayType.SelectedIndex.Equals(6))
            {
                tb_Other.Visible = true;
            }
            else
            {
                tb_Other.Visible = false;
            }
        }

        private void tb_TextChanged(object sender, EventArgs e)
        {
            if (tb_COG.Text != "" && tb_Gal.Text != "")
            {
                double x = Convert.ToDouble(tb_COG.Text.ToString());
                double y = Convert.ToDouble(tb_Gal.Text.ToString());

                double total = x * y;

                l_Total.Text = total.ToString();
            }
        }

        #region Make textboxes only accept number values
        private void tb_Odom_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (0x08 == a && tb_Odom.Text.Length != 0)
            {
                if (tb_Odom.Text[tb_Odom.Text.Length - 1] == '.')
                {
                    dPoint = false;
                }
            }

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a) | ((tb_Odom.TextLength >= 8 && dPoint) | (tb_Odom.TextLength >= 6 && !dPoint)))
            {
                if (a == '.' && !dPoint)
                {
                    dPoint = true;
                }
                else if (0x08 != a)
                {
                    e.Handled = true;
                }
            } // end if    
        }

        private void tb_COG_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (0x08 == a && tb_COG.Text.Length != 0)
            {
                if (tb_COG.Text[tb_COG.Text.Length - 1] == '.')
                {
                    COGDPoint = false;
                }
            }

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a) | ((tb_COG.TextLength >= 5 && COGDPoint) | (tb_COG.TextLength >= 1 && !COGDPoint)))
            {
                if (a == '.' && !COGDPoint)
                {
                    COGDPoint = true;
                }
                else if (0x08 != a)
                {
                    e.Handled = true;
                }
            } // end if   
        }

        private void tb_Gal_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (0x08 == a && tb_Gal.Text.Length != 0)
            {
                if (tb_Gal.Text[tb_Gal.Text.Length - 1] == '.')
                {
                    galDPoint = false;
                }
            }

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a) | ((tb_Gal.TextLength >= 8 && galDPoint) | (tb_Gal.TextLength >= 6 && !galDPoint)))
            {
                if (a == '.' && !galDPoint)
                {
                    galDPoint = true;
                }
                else if (0x08 != a)
                {
                    e.Handled = true;
                }
            } // end if   
        }
        #endregion
    }
}
